//
//  PersonInformVC.m
//  hnGovernment
//
//  Created by 安仔 on 17/3/6.
//  Copyright © 2017年 安仔. All rights reserved.
//

#import "PersonInformVC.h"
#import "ChangePhoneVC.h"
#import "ChangeNameVC.h"
#import "BindMailBoxVC.h"
#import "DDcacheManager.h"
#import "DDbaseNetWork+Login.h"
#import "DDcacheManager.h"
#import "NSString+mobileEncryption.h"
#import "HYImagePicker.h"
#import "MemberCenterCell.h"
@interface PersonInformVC ()<UITableViewDelegate, UITableViewDataSource,HYImagePickerDelegate>

@property (nonatomic, strong) UITableView * myTableView;
@property (nonatomic, strong) NSArray *tableViewArray;
@property (nonatomic,strong) UIImageView *headImage;
@end

@implementation PersonInformVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    ZWUserModel *model = [cacheManager getUserModel];
    if (!(model.email.length == 0)) {
        UITableViewCell *cell = [_myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2]];
        cell.detailTextLabel.text = model.email;
    }
    else { }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = NSLocalString(@"个人信息设置");
    self.view.backgroundColor = BackgroundColor;
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - 128 * m6Scale) style:UITableViewStyleGrouped];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    else if (section == 1) {
    
        return 3;
    }
    else {
        return 2;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
        return 140 * m6Scale;
    }
    else {
        return 102 * m6Scale;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 20 * m6Scale;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    if (section == 1) {
        return  10 * m6Scale;
    }
    else {
        return 0.01;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    MemberCenterCell * cell = [self.myTableView dequeueReusableCellWithIdentifier:@"personInformCell"];
    if (!cell) {
        cell = [[MemberCenterCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"personInformCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        if (indexPath.section == 0 ) {
            cell.rightInfotLab.hidden = YES;
            cell.signImg.image = [UIImage imageNamed:@"identity_header"];
            cell.titleLab.text = NSLocalString(@"头像");
            UIImageView *headImg = [[UIImageView alloc] init];
            self.headImage = headImg;
            headImg.image = [UIImage imageNamed:@"menberCenter_headerImage"];
            headImg.clipsToBounds = YES;
            headImg.backgroundColor = [UIColor whiteColor];
            headImg.layer.cornerRadius = 40 * m6Scale;
            headImg.clipsToBounds = YES;
            [cell addSubview:headImg];
            [headImg mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(cell.mas_centerY);
                make.right.equalTo(cell.mas_right).offset(-80 * m6Scale);
                make.size.mas_equalTo(CGSizeMake(80*m6Scale, 80*m6Scale));
            }];
        }
        else if (indexPath.section == 1) {
            
            if (indexPath.row == 0) {
                
                cell.signImg.image = [UIImage imageNamed:@"identity_phone"];
                cell.titleLab.text = NSLocalString(@"手机号码");
                ZWUserModel *model = [cacheManager getUserModel];
                NSString *mobilencryptString = [NSString stringWithFormat:@"%@",[model.mobile encryption]];
                cell.rightInfotLab.text = NSLocalString(mobilencryptString);
            }
            else if (indexPath.row == 1) {
                cell.signImg.image = [UIImage imageNamed:@"identity_level"];
                cell.titleLab.text = NSLocalString(@"实名等级");
                cell.rightInfotLab.text = NSLocalString(@"未实名认证");
                
            }
            else {
                cell.signImg.image = [UIImage imageNamed:@"identity_name"];
                cell.titleLab.text = NSLocalString(@"昵称");
                NSString *title = [NSString stringWithFormat:@"%@",[cacheManager getUserModel].displayName];
                cell.rightInfotLab.text = NSLocalString(title);
                
            }
        }
        else {
            
            if (indexPath.row == 0) {
                cell.signImg.image = [UIImage imageNamed:@"identity_email"];
                cell.titleLab.text = NSLocalString(@"邮箱");
                NSString * mail = [cacheManager getUserModel].email;
                if (mail == nil) {
                    mail = @"未绑定";
                }
                cell.rightInfotLab.text = NSLocalString(mail);
                
            }
            else {
                cell.signImg.image = [UIImage imageNamed:@"identity_address"];
                cell.titleLab.text = NSLocalString(@"地址");
                cell.rightInfotLab.text = NSLocalString(@"河南省郑州市建设路12号");
                
            }
        }

    }
    
    return cell;
}


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0 && indexPath.section == 0) {
        HYImagePicker *picker = [[HYImagePicker alloc]init];
        [picker showWith:self];
    }
    else if (indexPath.row == 0 && indexPath.section == 1) {
        
        ChangePhoneVC *vc = [[ChangePhoneVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    
    }
    else if (indexPath.row == 1 && indexPath.section == 1) {
        [SVProgressHUD showInfoWithStatus:@"敬请期待"];
    }

    else if (indexPath.row == 2 && indexPath.section == 1) {
        ChangeNameVC *vc = [[ChangeNameVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }

    else if (indexPath.row == 0 && indexPath.section == 2) {
        BindMailBoxVC *vc = [[BindMailBoxVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else {
        [SVProgressHUD showInfoWithStatus:@"敬请期待"];
    
    }

}

- (void)imagePicker:(HYImagePicker *)imagePicker type:(MediaType)type and:(NSString *)path {
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    self.headImage.image  = image;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
